/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Austin
 */
public class TimeFormat {

    //拆分年月日
    public static Map<String, Integer> getYMDH(long time) {
        Date d = new Date(time * 1000L);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        Map<String, Integer> map = new HashMap<>();
        map.put("year", c.get(Calendar.YEAR));
        map.put("month", c.get(Calendar.MONTH) + 1);
        map.put("day", c.get(Calendar.DAY_OF_MONTH));
        map.put("hour", c.get(Calendar.HOUR_OF_DAY));
        return map;
    }

    //时间转换
    public long getTimestamp(String sTime) {
        long rand = -99;
        try {
            Timestamp ts = Timestamp.valueOf(sTime);
            Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
            long l = ts.getTime() - date2.getTime() > 0 ? ts.getTime() - date2.getTime() : date2.getTime() - ts.getTime();
            rand = (int) (l / 1000);

        } catch (ParseException ex) {
            Logger.getLogger(TimeFormat.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return rand;
        }
    }

    public static String getTimeString(Long i) {
        Date d = new Date(i * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(d);
    }

    //获取当前系统时间
    public long getNewTime() {
        Long nowTime = System.currentTimeMillis() / 1000;
        return nowTime;
    }

    /* 
     * 将时间戳转换为时间,获取年月
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        long lt = new Long(s) * 1000;
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 时间转换时间戳（秒）
     *
     * @param 时间日期 格式yyyyMMddHHmmss
     * @return
     * @throws ParseException
     */
    public static Long dateToStamp(String s) throws ParseException {
        Long res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = simpleDateFormat.parse(s);
        res = date.getTime() / 1000;
        return res;
    }

    public static Long getMqTime(String s) throws ParseException {
        Long res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        res = date.getTime();
        return res;
    }

    public static void main(String[] args) throws ParseException, UnsupportedEncodingException, MalformedURLException {
        System.out.println(getTimeString(1502849398L));
    }
}
